package com.github.pig.admin.controller.sysFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.model.entity.SysFile;
import com.github.pig.admin.service.sysFile.SysFileService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Base64;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 上传文件 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-06-26
 */
@RestController
@RequestMapping("/sysFile")
public class SysFileController extends BaseController {
    @Autowired
    private SysFileService sysFileService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return SysFile
    */
    @GetMapping("/{id}")
    public R<SysFile> get(@PathVariable Integer id) {
        return new R<>(sysFileService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysFileService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 上传文件
     * @param  sysFile  实体
     * @return success/false
     */
    @PostMapping
    public String uploadFile(@RequestBody SysFile sysFile, MultipartFile file, RedirectAttributes redirectAttributes) {
        Base64.Encoder encoder  = Base64.getEncoder();
        return null;
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        SysFile sysFile = new SysFile();
        sysFile.setId(id);
        sysFile.setUpdateDate(new Date());
        sysFile.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(sysFileService.updateById(sysFile));
    }

    /**
     * 编辑
     * @param  sysFile  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody SysFile sysFile) {
        sysFile.setUpdateDate(new Date());
        return new R<>(sysFileService.updateById(sysFile));
    }
}
